#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>

#include "callbacks.h"
#include "interface.h"
#include "support.h"
#include "appdata.h"
#include "knots.h"

gboolean key_press_cb(GtkWidget *widget, GdkEventKey *key, GtkWindow *window) {
  if ((key->keyval) == GDK_F6) {
    being_handled = TRUE;
    toggle_fullscreen();
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(fullscreen), is_fullscreen);
    being_handled = FALSE;
    return TRUE;
  }
  else {
    return FALSE;
  }
}

void on_new1_activate (GtkMenuItem *menuitem, gpointer user_data) {
  do_new();
}

void on_new_tb(GtkToolButton *tbutton, gpointer user_data) {
  do_new();
}


void on_open1_activate (GtkMenuItem     *menuitem, gpointer user_data) {
 do_open();
}

void on_open_tb(GtkToolButton *tbutton, gpointer user_data) {
  do_open();
}


void on_save1_activate(GtkMenuItem *menuitem, gpointer user_data) {
  do_save();
}

void on_save_tb(GtkToolButton *tbutton, gpointer user_data) {
  do_save();
}


void on_save_as1_activate(GtkMenuItem *menuitem, gpointer user_data) {
  do_save_as();
}

void on_save_as_tb(GtkToolButton *tbutton, gpointer user_data) {
  do_save_as();
}


void on_quit1_activate(GtkMenuItem *menuitem, gpointer user_data) {
  if (is_dirty) {
    do_save_as();
  }
  gtk_main_quit();
}


void on_knots_menu(GtkMenuItem *menuitem, gpointer user_data) {
  if (!being_handled) {
    being_handled = TRUE;
    show_knots = TRUE;
    drawKnot();
    gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(radiotoolbutton1), TRUE);
    being_handled = FALSE;
  }
}

void on_splits_menu(GtkMenuItem *menuitem, gpointer user_data) {
  if (!being_handled) {
    being_handled = TRUE;
    show_knots = FALSE;
    drawKnot();
    gtk_toggle_tool_button_set_active(GTK_TOGGLE_TOOL_BUTTON(radiotoolbutton2), TRUE);
    being_handled = FALSE;
  }
}


void on_knots_tb(GtkToggleToolButton *tbutton, gpointer user_data) {
  if (!being_handled) {
    being_handled = TRUE;
    show_knots = TRUE;
    drawKnot();
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(knots1), TRUE);
    being_handled = FALSE;
  }
}

void on_splits_tb(GtkToggleToolButton *tbutton, gpointer user_data) {
  if (!being_handled) {
    being_handled = TRUE;
    show_knots = FALSE;
    drawKnot();
    gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(splits1), TRUE);
    being_handled = FALSE;
  }
}


void on_fullscreen_activate(GtkMenuItem *menuitem, gpointer user_data) {
  if (!being_handled) {
    toggle_fullscreen();
  }
}

void
on_zoom_in1_activate                   (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}


void
on_zoom_out1_activate                  (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}


void on_toolbar_toggle(GtkMenuItem *menuitem, gpointer user_data) {
  if (tb_visible) {
    gtk_widget_hide(toolbar2);
    tb_visible = FALSE;
  }
  else {
    gtk_widget_show(toolbar2);
    tb_visible = TRUE;
  }
}


void
on_help_on_knot1_activate              (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{

}


void
on_about1_activate                     (GtkMenuItem     *menuitem,
                                        gpointer         user_data)
{
	GtkWidget *dialog;
	//static gint i = 1;

	dialog = gtk_message_dialog_new (GTK_WINDOW (window),
				   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				   GTK_MESSAGE_INFO,
				   GTK_BUTTONS_OK,
				   "About...");
	//gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%d", i);
	gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);
	//i++;
}
gboolean button_press_event_callback (GtkWidget *widget, GdkEventButton *event, gpointer data) {
	GtkWidget *dialog;

	dialog = gtk_message_dialog_new (GTK_WINDOW (window),
				   GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
				   GTK_MESSAGE_INFO,
				   GTK_BUTTONS_OK,
				   "Click!");
	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "at %g, %g", event->x, event->y);
	gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);
	return FALSE;
}

gboolean expose_event_callback (GtkWidget *widget, GdkEventExpose *event, gpointer data) {
  int i, j, k, ty;

  ty = 0;
  if (!show_knots) {
    ty = TILEHEIGHT;
  }

  for (i = 0; i < kwidth; i++) {
    for (j = 0; j < kheight; j++) {
      k = knot[arraypos(i, j)];
      if (k < 100) {
        gdk_draw_pixbuf(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
                      tiles,
                      k * TILEWIDTH, ty,
                      i * TILEWIDTH, j * TILEHEIGHT,
                      TILEWIDTH, TILEHEIGHT,
                      GDK_RGB_DITHER_NONE, 0, 0);
      }
      else {
        gdk_draw_pixbuf(widget->window, widget->style->fg_gc[GTK_WIDGET_STATE(widget)],
                      tiles,
                      0, 0,
                      i * TILEWIDTH, j * TILEHEIGHT,
                      TILEWIDTH, TILEHEIGHT,
                      GDK_RGB_DITHER_NONE, 0, 0);
      }
    }
  }

  return TRUE;
}

